package cz.fabian.android.placeLocator;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DbAdapter {
	private static final int DATABASE_VERSION = 3;
    private static final String PLACE_TABLE_NAME = "place";
    private static final String DATABASE_NAME = "placeLocator";
    
    public static final String KEY_ROWID = "_id";
    public static final String KEY_TITLE = "title";
    public static final String KEY_LAT = "lat";
    public static final String KEY_LON = "lon";
    
    private final Context mCtx;    
    private DbHelper dbHelper;
    private SQLiteDatabase db;
    
	private class DbHelper extends SQLiteOpenHelper {
	    private static final String PLACE_TABLE_CREATE =
	                "CREATE TABLE " + PLACE_TABLE_NAME + " (" +
	                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
	                KEY_TITLE + " TEXT,"
	                + KEY_LAT + " FLOAT, " //FIXME: decimal
	                + KEY_LON + " FLOAT " //FIXME: decimal
	                + ");";

	    DbHelper(Context context) {
	        super(context, DATABASE_NAME, null, DATABASE_VERSION);
	    }

	    @Override
	    public void onCreate(SQLiteDatabase db) {
	        db.execSQL(PLACE_TABLE_CREATE);
	    }
	    
	    @Override
	    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	        Log.w("PlaceLocator", "Upgrading database from version " + oldVersion + " to "
	                + newVersion + ", which will destroy all old data");
	        db.execSQL("DROP TABLE IF EXISTS " + PLACE_TABLE_NAME);
	        onCreate(db);
	    }
	}
	
	public DbAdapter(Context ctx) {
		this.mCtx = ctx;		
	}
	
	public DbAdapter open() throws SQLException {
        dbHelper = new DbHelper(mCtx);
        db = dbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
    	dbHelper.close();
    }
    
  //FIXME: decimal lat lon
    public long create(String title, Float lat, Float lon) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_TITLE, title);
        initialValues.put(KEY_LAT, lat);
        initialValues.put(KEY_LON, lon);

        return db.insert(PLACE_TABLE_NAME, null, initialValues);
    }
    
    public Cursor fetchAll() {
        return db.query(PLACE_TABLE_NAME, new String[] {KEY_ROWID, KEY_TITLE}, null, null, null, null, null);
    }
    
    public Cursor fetch(long rowId) {
        Cursor cursor = db.query(true, PLACE_TABLE_NAME, new String[] {KEY_ROWID,
                KEY_TITLE, KEY_LAT, KEY_LON}, KEY_ROWID + "=" + rowId, null,
                null, null, null, null);
        if (cursor != null) {
        	cursor.moveToFirst();
        }
        
        return cursor;
    }
    
    public boolean delete(long rowId) {
    	return db.delete(PLACE_TABLE_NAME, KEY_ROWID + "=" + rowId, null) > 0;
	}
}
